package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.dao.ISeatDao;
import com.multiplayer.dao.impl.SeatDaoImpl;
import com.multiplayer.entity.Reservation;
import com.multiplayer.entity.Seat;
import com.multiplayer.service.IReservationService;
import com.multiplayer.service.impl.ReservationServiceImpl;


/**
 * Servlet implementation class ReservationServlet
 */
@WebServlet("/ReservationServlet")
public class ReservationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IReservationService rs=new ReservationServiceImpl();
	ISeatDao isd = new SeatDaoImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReservationServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1设置请求编码,我们将这个编码提取到filter中来。
		request.setCharacterEncoding("utf-8");
		// 标记当前为何种操作的变量
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if ("queryreservation".equals(op)) {
			System.out.println("salk");
			doQueryReservation(request, response);
		}else if("delreservation".equals(op)) {
			System.out.println("kala");
			doDelReservation(request, response);
		}
	}
	/*
	 * 封装查询预约方法
	 */
	private void doQueryReservation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userid = "";
		// 2获取请求参数
		if (request.getParameter("userid") != null) {
			userid = request.getParameter("userid");
		}

		// 3 调用 service的方法
		List<Reservation> list = rs.getAllReservation(userid);
		System.out.println("+++++++++");
		list.forEach(System.out::println);
		// 4传递list给jsp
		request.setAttribute("list", list);

		// 5 转发
		request.getRequestDispatcher("reservation.jsp").forward(request, response);
	}
	/*
	 * 封装取消预约信息方法
	 */
	private void doDelReservation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userid = "";
		// 2.获取请求参数
		if (request.getParameter("userid") != null) {
			userid = request.getParameter("userid");
		}
		// 3 调用 service的方法
		boolean flag=rs.delReservation(userid);
		
		String eseatno = request.getParameter("eseatno");
		String rno = isd.querySeatByThree(eseatno, "", "").get(0).getRno();
		String estatus = "n";
		Seat seat = new Seat(eseatno, rno, estatus );
		isd.editSeat(seat);
		
		
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		//4.响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);
		
		//5.关闭
		pw.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
